<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class productpic extends a_base {

    public $classpropertyid = 2;

    public function __construct() {
        parent::__construct();
        $this->load->model('m_productpic');
        $this->load->model('m_product');
    }

    public function index() {
        $productid = intval($this->input->get('productid'));
        $product = $this->m_product->get_product(array('productid' => $productid));
        if (empty($product)) {
            $vt_data = array(
                'js_fun' => 'tips',
                'param' => array('产品不存在', site_url('/product/index')),
            );
            $this->load->view('tips', $vt_data);
            return;
        }
        $orderkey_list = array('productpicid', 'productid', 'title', 'smallpic', 'bigpic', 'description', 'listid', 'adminid', 'addtime', 'isclose');
        $orderkey = $this->input->get('orderkey');
        if (!in_array($orderkey, $orderkey_list)) {
            $orderkey = 'productpicid';
        }
        $ascdesc = $this->input->get('ascdesc');
        if ($ascdesc != 'asc') {
            $ascdesc = 'desc';
        }
        $where = array('productid' => $productid);

        $title = $this->input->get('title');
        if (isset($_GET['title']) && !empty($title)) {
            $where['title'] = $title;
        }

        $isclose = $this->input->get('isclose');
        if (isset($_GET['isclose']) && $isclose != -1) {
            $where['isclose'] = $isclose;
        }

        $page = $this->input->get('page'); //当前页
        $page = intval($page);
        if ($page < 1) {
            $page = 1;
        }
        $cnt = $this->m_productpic->get_count($where);

        $page_size = 10;
        $arr = array(
            'page' => $page, //当前页
            'page_size' => $page_size, //分页总大小
            'total' => $cnt//总记录数
        );
        $start_page = ($page - 1) * $page_size;
        $data['pagination'] = new pagination($arr);

        $t_data = array(
            'where' => $where,
            'limit' => $page_size,
            'offset' => $start_page,
            'order' => $orderkey . ' ' . $ascdesc,
        );
        $data['list'] = $this->m_productpic->get_productpic_list($t_data);

        $this->load->model('m_admin');
        $data['admin'] = $this->m_admin->get_all(array('column' => 'adminname,adminid'));
        $data['product'] = $product;
        $where['orderkey'] = $orderkey;
        $where['ascdesc'] = $ascdesc;

        $data['where'] = $where;
        $this->load->view('productpic/index', $data);
    }

    public function add() {
        $productpicid = intval($this->input->get('productpicid'));

        $defalut = array(
            //  'productid' => 0,
            'title' => '',
            'smallpic' => '',
            'bigpic' => '',
            'description' => '',
            'listid' => 1,
            'isclose' => 0
        );

        if ($productpicid > 0) {
            $productpic = $this->m_productpic->get_productpic(array('productpicid' => $productpicid));
            if (empty($productpic)) {
                $vt_data = array(
                    'js_fun' => 'tips',
                    'param' => array('产品图片不存在', site_url('/product/index')),
                );
                $this->load->view('tips', $vt_data);
                return;
            }
            $data['post_url'] = b_url('productpic/add?productpicid=' . $productpicid);
            $data['productpic'] = $productpic;
        } else {
            $productid = intval($this->input->get('productid'));
            $product = $this->m_product->get_product(array('productid' => $productid));
            if (empty($product)) {
                $vt_data = array(
                    'js_fun' => 'tips',
                    'param' => array('产品不存在', site_url('/product/index')),
                );
                $this->load->view('tips', $vt_data);
                return;
            }
            $data['productpic'] = $defalut;
            $data['post_url'] = b_url('productpic/add?productid=' . $productid);
        }

        if (isset($_POST['btnSave'])) {
            $post = $this->input->post();
            $result = array();
            foreach ($defalut as $k => $v) {
                if (isset($post[$k])) {
                    $result[$k] = $post[$k];
                }
            }
            unset($result['configid']);
            if ($productpicid > 0) {
                $result['productpicid'] = $productpic['productpicid'];
                $this->m_productpic->update($result);
            } else {
                $result['adminid'] = 1;
                $result['addtime'] = date('Y-m-d H:i:s');
                $this->m_productpic->insert($result);
            }
            redirect('productpic/index');
            exit;
        }
        $this->load->view('productpic/add', $data);
    }

    function delete() {
        $productid = $this->input->get('productid');
        $productpicid = $this->input->get('productpicid');
        $list = explode('.', $productpicid);
        foreach ($list as $k => $v) {
            $v = intval($v);
            if ($v > 0) {
                $this->m_productpic->delete(array('productpicid' => $v));
            }
        }
        // 提示
        $vt_data = array(
            'js_fun' => 'tips',
            'param' => array('编号为' . str_replace('.', '，', $productpicid) . '产品图片删除成功!', b_url('productpic/index?productid=' . $productid), 1),
        );
        $this->load->view('tips', $vt_data);
    }

    function openclose() {
        $productid = $this->input->get('productid');
        $productpicid = $this->input->get('productpicid');
        $status = $this->input->get('status');
        $str_status = '关闭';
        if ($status != 1) {
            $status = 0;
            $str_status = '开放';
        }
        $list = explode('.', $productpicid);
        foreach ($list as $k => $v) {
            $v = intval($v);
            if ($v > 0) {
                $this->m_productpic->update(array('productpicid' => $v, 'isclose' => $status));
            }
        }
        // 提示
        $vt_data = array(
            'js_fun' => 'tips',
            'param' => array('编号为' . str_replace('.', '，', $productpicid) . '产品图片' . $str_status . '成功!', b_url('productpic/index?productid=' . $productid), 1),
        );
        $this->load->view('tips', $vt_data);
    }

}
